import request from "@/utils/request";
import { clearEmpty } from "@/utils/grpc";
import { useUserStore } from "@/stores/modules/user";
import type { AxiosPromise } from "axios";
import type {
  DictEntryApi,
  DictEntryReq,
  DictEntryModel,
  DictEntryPage,
  DictEntryList,
  DictEntryKey,
  DictEntryKeyList,
  DictEntryMoveKey,
  DictEntryEnableKey,
  DictEntryCreateReq,
  DictEntryUpdateReq,
  DictEntryExportReq
} from "./types";

class DictEntryApiImpl implements DictEntryApi {
  getKey(model: DictEntryModel): DictEntryKey {
    return {
      dictCode: model.dictCode,
      typeCode: model.typeCode
    } as DictEntryKey;
  }

  //查询翻页信息数据字典
  findPage(query?: DictEntryReq): AxiosPromise<DictEntryPage> {
    const tenant = useUserStore().user.tenant;
    if (query) {
      query.hasEnable = query.enable != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_entry/page/" + tenant,
      method: "get",
      params: query
    });
  }

  //查询列表信息数据字典
  findList(query?: DictEntryReq): AxiosPromise<DictEntryList> {
    const tenant = useUserStore().user.tenant;
    if (query) {
      query.hasEnable = query.enable != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_entry/list/" + tenant,
      method: "get",
      params: query
    });
  }

  //查询数据字典
  findById(query: DictEntryKey): AxiosPromise<DictEntryModel> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_entry/id/" + tenant + "/" + query.typeCode + "/" + query.dictCode,
      method: "get"
    });
  }

  // 创建实体数据字典
  create(data: DictEntryCreateReq): AxiosPromise<DictEntryModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    clearDictCacheAll(data.typeCode);
    return request({
      url: "/v1/dict/dict_entry",
      method: "post",
      data: data
    });
  }

  //修改实体数据字典
  update(data: DictEntryUpdateReq): AxiosPromise<DictEntryModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    clearDictCacheAll(data.typeCode);
    return request({
      url: "/v1/dict/dict_entry",
      method: "patch",
      data: data
    });
  }

  //上移下移
  move(key: DictEntryKey, up: boolean): AxiosPromise<boolean> {
    const data: DictEntryMoveKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.up = up;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_entry/move",
      method: "put",
      data: data
    });
  }

  //启用/禁用
  enable(key: DictEntryKey, enable: boolean): AxiosPromise<boolean> {
    const data: DictEntryEnableKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.enable = enable;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_entry/enable",
      method: "put",
      data: data
    });
  }

  // 删除数据字典
  delete(query: DictEntryKey): AxiosPromise<DictEntryModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    clearDictCacheAll(query.typeCode);
    return request({
      url: "/v1/dict/dict_entry/id",
      method: "delete",
      params: query
    });
  }

  // 批量删除数据字典
  batchDelete(keys: DictEntryKey[]): AxiosPromise<number> {
    const data = {
      tenant: useUserStore().user.tenant,
      dictEntryKey: keys
    } as DictEntryKeyList;
    clearEmpty(data);
    clearDictCacheAll(keys[0].typeCode);
    return request({
      url: "/v1/dict/dict_entry/batch",
      method: "put",
      data: data
    });
  }
}

const dictEntryApi = new DictEntryApiImpl();

//清理缓存.
export function clearDictCacheAll(typeCode?: string): void {
  if (typeCode) {
    clearDictCache(typeCode);
    clearDictCache(typeCode, true);
  }
}
//清理缓存.
export function clearDictCache(typeCode: string, enable?: boolean): void {
  const dictKey: string = `dict_${typeCode}_${enable}`;
  sessionStorage.removeItem(dictKey);
}

export { dictEntryApi };
